package com.dc.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dao.SysRoleMenuDao;
import com.dc.project.system.dto.SysRoleMenuDto;
import com.dc.project.system.entity.SysRoleMenu;
import com.dc.project.system.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色菜单关联表(SysRoleMenu)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-04 17:09:11
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenu> implements SysRoleMenuService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询角色菜单关联表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(SysRoleMenuDto dto) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        IPage<SysRoleMenu> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增角色菜单关联表
     *
     * @param sysRoleMenu
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SysRoleMenu sysRoleMenu) {
        return ResultVo.verify(this.save(sysRoleMenu));
    }

    /**
     * 编辑角色菜单关联表
     *
     * @param sysRoleMenu
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SysRoleMenu sysRoleMenu) {
        return ResultVo.verify(this.updateById(sysRoleMenu));
    }

    /**
     * 根据主键删除角色菜单关联表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 根据角色id、菜单id批量新增
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    public boolean addBatchRoleMenu(Integer roleId, List<Integer> menuIds) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        this.remove(queryWrapper);
        List<SysRoleMenu> roleMenuList = menuIds.stream().map(menuId -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            return roleMenu;
        }).collect(Collectors.toList());
        return this.saveBatch(roleMenuList, roleMenuList.size());
    }

    /**
     * 根据角色id获取菜单id
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getMenuIdByRoleId(Integer roleId) {
        return baseMapper.getMenuIdByRoleId(roleId);
    }

    /**
     * 根据角色id获取用户权限
     *
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> getMenuIdByRoleIds(Set<Integer> roleIds) {
        return baseMapper.getMenuIdByRoleIds(roleIds);
    }
}